package com.cxyzy.commonviewholderlib

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.text.TextWatcher
import android.util.SparseArray
import android.view.View
import android.widget.*

import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.recyclerview.widget.RecyclerView

/**
 * 通用 RecyclerView Holder
 */

class ViewHolder(private val mContext: Context, itemView: View) : RecyclerView.ViewHolder(itemView) {
    private val mSparseArray: SparseArray<View> = SparseArray()

    @Suppress("UNCHECKED_CAST")
    fun <T : View> getView(@IdRes viewId: Int): T {
        var view: View? = mSparseArray.get(viewId)
        if (view == null) {
            view = itemView.findViewById(viewId)     // 使用 RecyclerView.ViewHolder 保存的 itemView
            mSparseArray.put(viewId, view)
        }
        return view as T
    }


    /********************************** 常用函数  */

    fun setVisibility(viewId: Int, visibility: Int): ViewHolder {
        val view = getView<View>(viewId)
        view.visibility = visibility
        return this
    }

    fun setPadding(viewId: Int, left: Int, top: Int, right: Int, bottom: Int): ViewHolder {
        val view = getView<View>(viewId)
        view.setPadding(left, top, right, bottom)
        return this
    }

    fun setSelected(viewId: Int, selected: Boolean): ViewHolder {
        val view = getView<View>(viewId)
        view.isSelected = selected
        return this
    }

    fun setBackgroundColor(viewId: Int, @ColorInt color: Int): ViewHolder {
        val view = getView<View>(viewId)
        view.setBackgroundColor(color)
        return this
    }

    fun setBackgroundRes(viewId: Int, @DrawableRes resId: Int): ViewHolder {
        val view = getView<View>(viewId)
        view.setBackgroundResource(resId)
        return this
    }

    fun setBackgroundDrawable(viewId: Int, drawable: Drawable): ViewHolder {
        val view = getView<View>(viewId)
        view.background = drawable
        return this
    }

    fun setBackgroundBitmap(viewId: Int, bitmap: Bitmap): ViewHolder {
        val view = getView<View>(viewId)
        view.background = BitmapDrawable(mContext.resources, bitmap)
        return this
    }

    fun setEnabled(viewId: Int, enabled: Boolean): ViewHolder {
        val view = getView<View>(viewId)
        view.isEnabled = enabled
        return this
    }

    fun isEnabled(viewId: Int): Boolean {
        return getView<View>(viewId).isEnabled
    }


    /********************************** 常用监听  */

    fun setOnClickListener(viewId: Int, listener: View.OnClickListener): ViewHolder {
        val view = getView<View>(viewId)
        view.setOnClickListener(listener)
        return this
    }

    fun setOnTouchListener(viewId: Int, listener: View.OnTouchListener): ViewHolder {
        val view = getView<View>(viewId)
        view.setOnTouchListener(listener)
        return this
    }

    fun setOnLongClickListener(viewId: Int, listener: View.OnLongClickListener): ViewHolder {
        val view = getView<View>(viewId)
        view.setOnLongClickListener(listener)
        return this
    }

    /**
     * 设置 RecyclerView item 点击监听
     *
     * @param listener
     * @return
     */
    fun setOnItemClickListener(listener: View.OnClickListener): ViewHolder {
        itemView.isClickable = true
        itemView.setOnClickListener(listener)
        return this
    }

    /**
     * 设置 RecyclerView item 长按监听
     *
     * @param listener
     * @return
     */
    fun onItenLongClickListener(listener: View.OnLongClickListener): ViewHolder {
        itemView.isLongClickable = true
        itemView.setOnLongClickListener(listener)
        return this
    }


    /********************************** TextView 相关函数  */

    fun setText(viewId: Int, value: CharSequence): ViewHolder {
        val view = getView<TextView>(viewId)
        view.text = value
        return this
    }

    fun setTextSize(viewId: Int, testSize: Int): ViewHolder {
        val view = getView<TextView>(viewId)
        view.textSize = testSize.toFloat()
        return this
    }

    fun setTextColor(viewId: Int, @ColorInt textColor: Int): ViewHolder {
        val view = getView<TextView>(viewId)
        view.setTextColor(textColor)
        return this
    }

    fun setTextColorRes(viewId: Int, @ColorRes color: Int): ViewHolder {
        val view = getView<TextView>(viewId)
        view.setTextColor(mContext.resources.getColor(color))
        return this
    }

    fun setTextDrawable(viewId: Int, left: Drawable, top: Drawable, right: Drawable, bottom: Drawable): ViewHolder {
        val textView = getView<TextView>(viewId)
        textView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
        return this
    }

    fun addTextChangedListener(viewId: Int, watcher: TextWatcher): ViewHolder {
        val editText = getView<EditText>(viewId)
        editText.addTextChangedListener(watcher)
        return this
    }


    /********************************** CheckBox 相关函数  */

    fun setOnCheckedChangeListener(viewId: Int, listener: CompoundButton.OnCheckedChangeListener): ViewHolder {
        val checkBox = getView<CheckBox>(viewId)
        checkBox.setOnCheckedChangeListener(listener)
        return this
    }

    fun setChecked(viewId: Int, checked: Boolean): ViewHolder {
        val view = getView<CheckBox>(viewId)
        view.isChecked = checked
        return this
    }

    fun isChecked(viewId: Int): Boolean {
        val checkBox = getView<CheckBox>(viewId)
        return checkBox.isChecked
    }


    /********************************** ProgressBar 相关函数  */

    fun setMax(viewId: Int, max: Int): ViewHolder {
        val view = getView<ProgressBar>(viewId)
        view.max = max
        return this
    }

    fun setPressed(viewId: Int, pressed: Boolean): ViewHolder {
        val view = getView<View>(viewId)
        view.isPressed = pressed
        return this
    }


    /********************************** ImageView 相关函数  */

    fun setImageResource(viewId: Int, imageResId: Int): ViewHolder {
        val view = getView<ImageView>(viewId)
        view.setImageResource(imageResId)
        return this
    }

    fun setImageDrawable(viewId: Int, drawable: Drawable): ViewHolder {
        val view = getView<ImageView>(viewId)
        view.setImageDrawable(drawable)
        return this
    }

    fun setImageBitmap(viewId: Int, bitmap: Bitmap): ViewHolder {
        val view = getView<ImageView>(viewId)
        view.setImageBitmap(bitmap)
        return this
    }
}